package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.PageBean;
import com.hmall.feign.client.SearchClient;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.mapper.SearchMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.ItemService;
import com.hmall.search.dto.SearchDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    SearchClient sClient;
    @Override

    public PageBean<Item> pageQuery(Integer page, Integer size) {
        //准备两个参数
        //参数1：分页对象Page  参数2：查询条件
        Page<Item> p = new Page<>(page, size);

       QueryWrapper wrapper= Wrappers.query().orderByDesc("create_time","update_time","id");
        //调用mapper，查询数据库，分页查询
        itemMapper.selectPage(p, wrapper);
        List<Item> items = new PageBean(p.getTotal(), p.getRecords()).getList();
        //int s=sClient.additem(items);
        //System.out.println(s);
        //返回数据
        return new PageBean(p.getTotal(), p.getRecords());

    }

    @Override
    public PageBean<Item> pageQuery(Integer page, Integer size, String sortBy) {
        Page<Item> p = new Page<>(page, size);
        //QueryWrapper<Item> wrapper = Wrappers.<Item>emptyWrapper().orderByDesc("create_time","update_time","id");
        if(sortBy.equals("default")){
            QueryWrapper wrapper= Wrappers.query().orderByDesc("create_time","update_time","id");
            //调用mapper，查询数据库，分页查询
            itemMapper.selectPage(p, wrapper);

            //返回数据
            return new PageBean(p.getTotal(), p.getRecords());
        }
        else if(sortBy.equals("sold")){
            QueryWrapper wrapper= Wrappers.query().orderByDesc("sold");
            //调用mapper，查询数据库，分页查询
            itemMapper.selectPage(p, wrapper);

            //返回数据
            return new PageBean(p.getTotal(), p.getRecords());
        }
        else if(sortBy.equals("price")){
            QueryWrapper wrapper= Wrappers.query().orderByDesc("price");

            //调用mapper，查询数据库，分页查询
            itemMapper.selectPage(p, wrapper);

            //返回数据
            return new PageBean(p.getTotal(), p.getRecords());
        }
       return null;

    }

    @Override
    public void updateItem(Item item) {

        item.setUpdateTime(new Date());
        itemMapper.updateById(item);
    }

    @Override
    public void addItem(Item item) {

        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        itemMapper.insert(item);
    }

    @Override
    public void updateStatus(Item item) {
        item.setUpdateTime(new Date());
        itemMapper.updateById(item);
    }

    @Override
    public void deleteById(Long id) {
        itemMapper.deleteById(id);
    }

    @Override
    public List<String[]> filterList() {
        List<String[]> list = new ArrayList<String[]>();
        String[]category = new String[]{};
        String[]brand = new String[]{};
        category= itemMapper.selectCategory();
        brand= itemMapper.selectBrand();
        list.add(category);
        list.add(brand);
        return list;

    }

    @Override
    public Item getById(Long id) {
        return itemMapper.selectById(id);
    }

    @Override
    public void updatestock(Long itemId, Integer num) {
      Item item=  getById(itemId);
      item.setSold(item.getSold()+num);
      item.setStock(item.getStock()-num);
      itemMapper.updateById(item);
    }


}
